package com.example.filter.slice;

import com.example.filter.*;
import com.zomato.photofilters.SampleFilters;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, "MainAbilitySlice");
    private AbilitySlice abilitySlice;
    private ListContainer thumbListView;
    private Image placeHolderImageView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        abilitySlice = this;
    }

    private void initUIWidgets() {
        thumbListView = (ListContainer) findComponentById(ResourceTable.Id_thumbnails);
        placeHolderImageView = (Image) findComponentById(ResourceTable.Id_place_holder_imageView);
        placeHolderImageView.setPixelMap(ResourceTable.Media_photo);
        initHorizontalList();
    }

    private void initHorizontalList() {
        thumbListView.setOrientation(Component.HORIZONTAL);
        bindDataToProvider();
        thumbListView.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component,
                                      int i, long l) {
                ThumbnailItem item = (ThumbnailItem) listContainer.getItemProvider().getItem(i);
                Image image = new Image(getContext());
                image.setPixelMap(ResourceTable.Media_photo);
                placeHolderImageView.setPixelMap(item.filter.processFilter(image.getPixelMap(), false));
            }
        });
    }

    private void bindDataToProvider() {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                Image image = new Image(getContext());
                image.setPixelMap(ResourceTable.Media_photo);
                PixelMap thumbImage = image.getPixelMap();

                ThumbnailItem t1 = new ThumbnailItem();
                ThumbnailItem t2 = new ThumbnailItem();
                ThumbnailItem t3 = new ThumbnailItem();
                ThumbnailItem t4 = new ThumbnailItem();
                ThumbnailItem t5 = new ThumbnailItem();
                ThumbnailItem t6 = new ThumbnailItem();

                t1.image = thumbImage;
                t2.image = thumbImage;
                t3.image = thumbImage;
                t4.image = thumbImage;
                t5.image = thumbImage;
                t6.image = thumbImage;
                ThumbnailsManager.clearThumbs();
                ThumbnailsManager.addThumb(t1);

                t2.filter = SampleFilters.getStarLitFilter();
                ThumbnailsManager.addThumb(t2);

                t3.filter = SampleFilters.getBlueMessFilter();
                ThumbnailsManager.addThumb(t3);

                t4.filter = SampleFilters.getAweStruckVibeFilter();
                ThumbnailsManager.addThumb(t4);

                t5.filter = SampleFilters.getLimeStutterFilter();
                ThumbnailsManager.addThumb(t5);

                t6.filter = SampleFilters.getNightWhisperFilter();
                ThumbnailsManager.addThumb(t6);

                List<ThumbnailItem> thumbs = ThumbnailsManager.processThumbs();

                ThumbnailProvider provider = new ThumbnailProvider(thumbs, abilitySlice);
                thumbListView.setItemProvider(provider);
                provider.notifyDataChanged();
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
        initUIWidgets();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
